<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/8
 * Time: 16:28
 */

namespace App\Rep;


use App\Models\Color;
use App\Models\ProductColor;

class ColorRep
{

    /**
     * 获取颜色列表
     * @param $params
     * @return mixed
     */
    public function getColorListRep($params){
        $list = Color::Search($params)->with(['product'])->orderBy('id','desc')->paginate($params['per_page']??10);
        return $list;
    }

    /**
     * 添加颜色
     * @param $params
     * @return Color|array
     */
    public function createColorRep($params){
        foreach($params['product_ids'] as $k=>$v){
            foreach($params['color_names'] as $kk=>$vv){
                $obj = Color::updateOrCreate(['color'=>$vv],['color'=>$vv,'is_del'=>2]);
                ProductColor::updateOrCreate(['color_id'=>$obj->id,'product_id'=>$v],['color_id'=>$obj->id,'product_id'=>$v]);
            }
        }
        return ['code'=>0,'data'=>[],'msg'=>'success'];
    }

    /**
     * 删除颜色
     * @param $params
     */
    public function delColorRep($params){
        $model = Color::where('id',$params['id'])->first();
        $model->is_del = $params['is_del'];
        $model->save();
        ProductColor::where('color_id',$model->id)->delete();
        return $model;
    }


}